package com.kgc.storeprovider.controller;

import com.kgc.basic.pojo.Good;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.store.pojo.InStore;
import com.kgc.store.pojo.OutStore;
import com.kgc.store.pojo.ProMove;
import com.kgc.store.remote.ProMoveRemote;
import com.kgc.storeprovider.service.ProMoveService;
import com.kgc.storeprovider.service.StoreGoodSummaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class ProMoveController implements ProMoveRemote {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ProMoveService proMoveService;
    @Autowired
    private StoreGoodSummaryService storeGoodSummaryService;
    @Override
    public ResponseMsg getOne(@RequestParam("code") String code) {
        logger.info("ProMoveController getOne code:"+code);
        ResponseMsg responseMsg = proMoveService.selectByCode(code);
        logger.debug("proMoveService selectByCode responseMsg:"+responseMsg+",code:"+code);
        return responseMsg;
    }

    @Override
    public ResponseMsg getPage(@RequestBody Map<String, Object> map){
        logger.info("ProMoveController getPage map:"+map);
        ResponseMsg responseMsg = proMoveService.selectByPage(map);
        logger.debug("proMoveService selectByPage responseMsg:"+responseMsg+",map:"+map);
        return responseMsg;
    }

    @Override
    public ResponseMsg add(@RequestBody ProMove proMove){
        logger.info("ProMoveController add proMove:"+proMove);
        ResponseMsg responseMsg = proMoveService.add(proMove);
        logger.debug("proMoveService add responseMsg:"+responseMsg+",proMove:"+proMove);
        return responseMsg;
    }

    @Override
    public ResponseMsg update(@RequestBody ProMove proMove){
        logger.info("ProMoveController update proMove:"+proMove);
        ResponseMsg responseMsg = proMoveService.update(proMove);
        logger.debug("proMoveService update responseMsg:"+responseMsg+",proMove:"+proMove);
        return responseMsg;
    }

    @Override
    public ResponseMsg del(@RequestParam("code") String code) {
        logger.info("ProMoveController del code:"+code);
        ResponseMsg responseMsg = proMoveService.del(code);
        logger.debug("proMoveService del responseMsg:"+responseMsg+",code:"+code);
        return responseMsg;
    }

    @Override
    public ResponseMsg changStatus(@RequestParam("code") String code,@RequestParam("status") String status) {
        logger.info("ProMoveController changStatus code:"+code+",status:"+status);
        ResponseMsg responseMsg = proMoveService.changeStatus(code,status);
        logger.debug("proMoveService step responseMsg:"+responseMsg);
        return responseMsg;
    }
    @Override
    public ResponseMsg moveStore(ProMove move) {
        ResponseMsg responseMsg = new ResponseMsg();
        // 1. 调拨出库
        OutStore out = proMoveService.createOutStoreFromProMove(move);

        // 2. 调拨入库
        InStore in  = proMoveService.createInStoreFromProMove(move);

        // 3. 更新库存：出库扣减 + 入库增加
        Map<String,Object> m = new HashMap<>();
        List<Good> goods = move.getGoods();
        List<Integer> counts = move.getCounts();
        List<Integer> osids  = move.getOsids();
        List<Integer> isids  = move.getIsids();

        // 出库扣减
        for (int i = 0; i < goods.size(); i++) {
            m.clear();
            m.put("stock", counts.get(i) * -1);
            m.put("gid", goods.get(i).getId());
            m.put("sid", osids.get(i));
            storeGoodSummaryService.method(m);
        }

        // 入库增加
        for (int i = 0; i < goods.size(); i++) {
            m.clear();
            m.put("stock", counts.get(i));
            m.put("gid", goods.get(i).getId());
            m.put("sid", isids.get(i));
            storeGoodSummaryService.method(m);
        }
        responseMsg.setCode("200");
        return responseMsg;
    }
}
